

package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class ProductOptionManagement {
    
    def listProductOptions() {
        def productOptions = ProductOption.list()
        JSONArray arr = new JSONArray()
        productOptions.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("optionType",it.optionType)
            obj.put("optionTypeId",it.optionTypeId)
            obj.put("productId",it.productId)
            arr.add(obj)
        }
        
        return arr
    }
    
    def listProductOptionsByProductID(productId) {   
//        println "in the management:: " + productId
//        println productId.class
        def productOptions = ProductOption.findAll()
        JSONArray arr = new JSONArray()
        productOptions.each() {
            String currentProductId = it.productId.toString()
//            println currentProductId.class
            if (currentProductId.equals(productId)) {
                JSONObject obj = new JSONObject()
                obj.put("id",it.id)
                obj.put("optionType",it.optionType)
                obj.put("optionTypeId",it.optionTypeId)
                obj.put("productId",it.productId)
                arr.add(obj)
            }
        }
        
        return arr
    }
    
    def getProductOptionByID(id) {
        def pt = ProductOption.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",pt.id)
        obj.put("optionType",pt.optionType)
        obj.put("optionTypeId",pt.optionTypeId)
        obj.put("productId",pt.productId)
        
        return obj
    }
    
    def getProductOptionByProductOptionType(optionType) {
        def pt = ProductOption.findByProductOptionType(optionType)
        JSONObject obj = new JSONObject()
        obj.put("id",pt.id)
        obj.put("optionType",pt.optionType)
        obj.put("optionTypeId",pt.optionTypeId)
        obj.put("productId",pt.productId)
        
        return obj
    }
    
    def getProductOptionForDbByProductOptionType(optionType) {
        def pt = ProductOption.findByProductOptionType(optionType)
        
        return pt
    }
    
    def addProductOption(o) {
        def status
        
        ProductOption pt = new ProductOption()
        pt.optionType = o.get("optionType")
        pt.optionTypeId = o.get("optionTypeId")
        if (pt.save()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add Product Option >> ' + status
        
        return status
    }
    
    def updateProductOption(productOption) {
        def status
        
        ProductOption pt = ProductOption.findById(productOption.id)
        if (productOption.has("optionType")) { pt.optionType = productOption.optionType }
        if (productOption.has("optionTypeId")) { pt.optionTypeId = productOption.optionTypeId }
        if (pt.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update Product Option >> ' + status
        
        return status
    }
    
    def deleteProductOption(id) {
        def status
        
        ProductOption pt = ProductOption.findById(id)
        if (pt.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete Product Option >> ' + status
        
        return status
    }
}

